package org.jfsm.demo.ccounter.echo;

import org.jfsm.core.ConcurrentFSM;
import org.jfsm.core.FSMReplyContext;
import org.jfsm.core.RetSignal;

public class EchoFSM extends ConcurrentFSM<EchoContext, EchoRequest, EchoResponse> {

    enum State {
        INIT, RUN
    }

    @Override
    protected void build(TransitionMap tm) {
        tm.setInitialState(State.INIT);

        tm.put(State.INIT, in.init, State.RUN, shInit);
        tm.put(State.RUN, in.ping, State.RUN, ehEcho);
    }

    SH<Object> shInit = new SH<Object>() {
        public RetSignal handle(EchoContext ctx, Object msg, FSMReplyContext replyCtx) {
            ctx.replyCtx = replyCtx;
            return null;
        };
    };

    EH ehEcho = new EH() {
        public RetSignal handle(EchoContext ctx) {
            out.pong.dispatch(ctx.replyCtx, ctx.id);
            return null;
        };
    };

}
